package ihm.panel.camera;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;

import ihm.applet.KrakenApplet;
import ihm.panel.KrakenTablePanel;
import ihm.panel.PanelInterface;
import ihm.panel.node.NodePanel;
import ihm.panel.site.CipherPanel;
import ihm.panel.video.FluxPanel;

import javax.swing.JPanel;
import javax.swing.JSplitPane;

/**
 * Panneau qui gère l'affichage des sous panneaux Vidéo, Flux et Tâches.
 * @author Guillaume Pocas & Jean-Fraçois Augé
 */
public class CameraPanel extends JPanel implements PanelInterface {

	private static final long serialVersionUID = 1L;
	private KrakenApplet applet;
	
	private JSplitPane main_jsp;
	private NodePanel node_jp;
	private JPanel card_jp;
	
	private FluxPanel flux_jp;
	private VideoPanel video_jp;
	private CameraOptionsPanel task_jp;
	//private QoSPanel qos_jp;
	private CipherPanel cipher_jp;
	//private StockagePanel stock_jp;
	
	/**
	 * Constructeur.
	 * @param applet
	 */
	public CameraPanel(KrakenApplet applet) {
		this.applet = applet;
		initPanel();
	}

	@Override
	public void initPanel() {

		setLayout(new BorderLayout());
		
		node_jp = new NodePanel(applet, this);
		
		card_jp = new JPanel();
		card_jp.setLayout(new CardLayout());
		
		flux_jp = new FluxPanel(applet);
		video_jp = new VideoPanel(applet,this);
		task_jp = new CameraOptionsPanel(applet,this);
		//qos_jp = new QoSPanel(applet);
		cipher_jp = new CipherPanel(applet, this);
		//stock_jp = new StockagePanel(applet);
		
		card_jp.add(new JPanel(), "Default");
		card_jp.add(flux_jp, "Flux");
		card_jp.add(video_jp, "Vidéos");
		card_jp.add(task_jp, "Tâches");
		//card_jp.add(qos_jp, "Qualité de service");
		card_jp.add(cipher_jp, "Chiffrement");
		//card_jp.add(stock_jp, "Stockage");
		
		main_jsp = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, node_jp, card_jp);
		//main_jsp.setOneTouchExpandable(true);
		main_jsp.setDividerLocation(200);
		add(main_jsp, BorderLayout.CENTER);
	}
	
	/**
	 * Change le panneau affiché.
	 * @param panelName
	 */
	public void setPanel(String panelName) {
		((CardLayout) card_jp.getLayout()).show(card_jp, panelName);

		JPanel panel = getCurrentPanel();
		if(panel instanceof KrakenTablePanel) {
			if(panel.isShowing()){
				KrakenTablePanel pan = (KrakenTablePanel) panel;
				pan.updateTableInfo();
				pan.getAppletTableModel().setData(pan.getColumnContent());
				pan.getAppletTableModel().fireTableDataChanged();
			}
		}
		else if(panel instanceof CipherPanel) {
			if(panel.isShowing()) {
				((CipherPanel) panel).updateTableInfo();
				((CipherPanel) panel).setSelectedEncryption();
			}
		}
		else if(panel instanceof FluxPanel) {
			if(panel.isShowing()) {
				String ip = node_jp.getCurrentCamera().getCam_ip();
				((FluxPanel) panel).getMediaPlayer().playMedia(ip);
			}
		}
	}
	
	/**
	 * Retourne le panneau affiché.
	 * @return
	 */
	public JPanel getCurrentPanel() {
        JPanel currentPanel = null;

        for(Component component : card_jp.getComponents()) {
        	if(component instanceof JPanel && component.isVisible()) {
                currentPanel = (JPanel) component;
            }
        }

        return currentPanel;
    }
	
	/**
	 * Retourne l'arborescence des sites et caméras.
	 * @return
	 */
	public NodePanel getNodePanel() { return this.node_jp; }
	
}
